//StudentController.java
package org.example.demo3.controller;

import org.example.demo3.model.Student;
import org.example.demo3.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller//@Controller:定义控制器类，处理HTTP请求
@RequestMapping("/Student")//@RequestMapping:映射请求路径和HTTP方法
public class StudentController {
    @Autowired //spring框架的注解，自动装配(注入)，使用StudentService对象
    StudentService studentService;
    @RequestMapping("list")
    public String list(Model model) {
        //调用studentMapper的selectAll,返回数据库student的数据
        List<Student> students = studentService.findAll();
        model.addAttribute("students", students);
        return "StudentsList";
//        System.out.println("welcome");//在控制台上打印
//        return "addStudent";//跳转到addStudent.html页面,全路径为/Student/addStudent
    }

    //万能转向页面(通过"page"传递参数，跳转到任意页面),例如：跳转login.html，→ /Student/toPage?page=login
    @RequestMapping("toPage")
    public String toPage(String page) {
        return page;
    }
    //登录验证
//    @RequestMapping("loginValidate")
//    //通过loginValidate获取用户输入的username、password，再由该方法进行页面跳转
//    public String loginValidate(Student student, Model model, HttpSession session, HttpServletResponse response) {
//        System.out.println(student.getName() + " " + student.getPassword());
//        //将数据存入model对象，准备渲染到welcome页面
////        model.addAttribute("username", username);
////        model.addAttribute("password", password);
//
////        model.addAttribute("student", student);
//
//        //服务器往客户端写一个cookie，它的名字是username，值是student.getUsername
//        response.addCookie(new Cookie("username",student.getName()));
//
//        session.setAttribute("username", student.getName());
//
//        //list类型的数据传递
//        List students = new ArrayList();
//        students.add(student);
//        //新建2个学生对象
////        Student s1 = new Student("tom","1");
////        Student s2 = new Student("jerry","2");
////        students.add(s1);
////        students.add(s2);
//        // 将students列表添加到Model中
//        model.addAttribute("students", students);
//
//        return "StudentsList";
//    }

    //删除学生
    @RequestMapping("remove")
    public String remove(int id) {
        studentService.remove(id);
        return "redirect:/Student/list/1/3";
    }

    //添加学生
    @RequestMapping("add")
    public String add(Student student) {
        studentService.add(student);
        return "redirect:/Student/list/1/3";
    }

    //准备修改 & 查询对应id的学生
    @RequestMapping("toEdit")
    public String toEdit(int id, Model model) {
        Student oldStudent = studentService.findOneById(id);
        model.addAttribute("student", oldStudent);
        return "editStudent";
    }

    //修改学生
    @RequestMapping("edit")
    public String edit(Student student) {
        studentService.edit(student);
        return "redirect:/Student/list/1/3";
    }

    //分页显示
    @RequestMapping("list/{currentPage}/{pageSize}")
    public String list(Model model,@PathVariable("currentPage") int currentPage,@PathVariable("pageSize") int pageSize) {
        if(currentPage < 1) currentPage = 1;
        if(pageSize < 1) pageSize = 10;
        List<Student> students = studentService.findByPage(currentPage,pageSize);
        model.addAttribute("students", students);
        return "StudentsList";
    }

}
